package com.sanerzone.smscenter.gateway.http.handler.listener.Task;

import com.alibaba.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import com.alibaba.rocketmq.common.message.MessageExt;
import com.sanerzone.common.support.utils.FstObjectSerializeUtil;
import com.sanerzone.smscenter.biz.message.SMSMTMessage;
import com.sanerzone.smscenter.biz.message.SMSSRMessage;
import com.sanerzone.smscenter.gateway.base.GatewayFactory;
import com.sanerzone.smscenter.gateway.base.Result;
import com.sanerzone.smscenter.gateway.http.handler.listener.GateWayMTListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class Task implements Runnable{
    public Task(){}
    private List<MessageExt> msgs;
    private ConsumeConcurrentlyContext context;
    private GatewayFactory gatewayFactory;
    Logger logger = LoggerFactory.getLogger(Task.class);

    public List<MessageExt> getMsgs() {
        return msgs;
    }

    public void setMsgs(List<MessageExt> msgs) {
        this.msgs = msgs;
    }

    public Task(List<MessageExt> msgs, ConsumeConcurrentlyContext context,GatewayFactory gatewayFactory){
        this.msgs=msgs;
        this.context=context;
        this.gatewayFactory=gatewayFactory;
    }

    @Override
    public void run() {
        Result result = null;
        for (MessageExt message : msgs){
            logger.info("http listener recv mt, topic:{}, tag:{}, msgid:{}, key:{}", message.getTopic(), message.getTags(), message.getMsgId(), message.getKeys());
            SMSMTMessage smsMtMessage = null;
            try
            {
                smsMtMessage = (SMSMTMessage) FstObjectSerializeUtil.read(message.getBody());
                if (smsMtMessage != null)
                {
                    long time1=System.currentTimeMillis();
                    result = gatewayFactory.sendMsg(smsMtMessage);
                    long time2=System.currentTimeMillis();
                    logger.info( "发送总共耗时{}",(time2-time1)+"====================" );
                }
                else
                {
                    result = new Result("F10106", "消息解析无数据");
                    smsMtMessage = new SMSMTMessage();
                    smsMtMessage.setId(message.getKeys());
                }
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                result = new Result("F10107", "消息解析异常");
                smsMtMessage = new SMSMTMessage();
                smsMtMessage.setId(message.getKeys());
            }
            //失败，放入队列
            if (!result.isSuccess())
            {
                SMSSRMessage smsSrMessage = new SMSSRMessage(message.getMsgId(), result.getErrorCode(), smsMtMessage);
                smsSrMessage.setReserve(result.getErrorMsg());
                try
                {
                    this.gatewayFactory.sendSmsSRMessage(smsSrMessage, smsMtMessage.getGatewayId());
                }
                catch (Exception e)
                {
                    logger.error("msgid:{}, key:{}, 消息解析异常:{}", message.getMsgId(), message.getKeys(), e.getMessage());
                }
            }
        }
    }
}
